--
-- Author: yawei.zhu
-- Date: 2018-05-10 16:54:36
--
-- DailyTaskRender
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskSideItemSubNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())

    self._ui.btn_refresh:addClickEventListener(function()
        self._ui.btn_refresh:setVisible(false) -- 防止连续点击
        executeFunc(self._onClick, "refreshDailyTask", self._data)
    end)

    self:refresh()
end

function ClassRef:setTaskType(taskType)
    self._taskType = taskType
end

function ClassRef:setDropItemNode(root, dropItem)
    local panelRoot = root:getChildByName("Panel")
    if dropItem then
        root:setVisible(true)
        local uiItem = IconRender.create(root)
        uiItem:setData(dropItem)
        uiItem:setTipEnable(true)
    else
        root:setVisible(false)
    end
end

function ClassRef:refresh()
    if not self._data then return end

    local task = self._data
    local taskId = task:getId()

    local rootPanel = self._ui["Panel"]
    local panelBG = rootPanel:getChildByName("Panel_bg")
    local btnItem = panelBG:getChildByName("bg01")
    local grade = panelBG:getChildByName("bg04")
    btnItem:setZoomScale(0)

    local qualityColor = Enums.QualityColor[tonumber(task:getConfig().quality) or 1]
    grade:setColor(qualityColor)

    task:setTaskIcon(iconNode)

    for i = 1, 2 do
        local name = "icon_reward"
        name = (i > 1) and name .. "_" .. ((i - 1) ) or name
        local rewardNode = rootPanel:getChildByName(name)
        local dropLists = task:getRewards()
        self:setDropItemNode(rewardNode, dropLists[i])
    end

    local iconOverNode = rootPanel:getChildByName("icon_over")
    local btnGo = rootPanel:getChildByName("Button_go")
    local btnTrack = rootPanel:getChildByName("selected_btn")
    local btnGet = rootPanel:getChildByName("Button_lingqu")
    local nameNode = rootPanel:getChildByName("name")
    local desNode = rootPanel:getChildByName("Text")
    local numNode = rootPanel:getChildByName("num")
    numNode:setVisible(false)
    btnGo:setVisible(false)
    btnTrack:setVisible(false)
    btnGet:setVisible(false)
    iconOverNode:setVisible(false)

    local btnName = btnGo:getChildByName("name")
    btnName:setString(L("task_daily_desc1"))

    local taskConfig = task:getConfig()

    nameNode:setColor(qualityColor)
    nameNode:setString(L(taskConfig.name))
    desNode:setString(L(taskConfig.target))

    if task:isTaken() then
        btnGo:setVisible(true)
        -- 设置进度
        local doneProgress = task:getDefaultDone()
        local totalProgress = task:getDefaultTotal()
        numNode:setVisible(true)
        numNode:setString(doneProgress .. "/" .. totalProgress)
    elseif task:isFinished() then
        btnGet:setVisible(true)
    elseif task:isRewarded() then
        iconOverNode:setVisible(true)
    end

    btnGo:addClickEventListener(function(sender)
        if tonumber(taskConfig.scheduleString) == 5 then -- 界面追踪
            UIHandler:handleMessage({id = tonumber(taskConfig.autoRun), mode = "open"})
        else
            self._onClick("TASK_AUTO_WAY", {findNpcId = taskConfig.autoRun, trackTaskId = taskId})
        end
    end)
    btnGet:addClickEventListener(function(sender)
        btnGet:setVisible(false) -- 防止连续点击
        self._onClick("GET_TASK_REWARD", {taskId = taskId, taskType = self._taskType})
    end)

    -- 每日任务 并且是taken状态，显示刷新按钮
    self._ui.btn_refresh:setVisible(false)
    if self._taskType == "daily" and task:isTaken() then
        local pos = task:getPos()
        local refreshNum = TaskManager:getDailyTaskRefreshNum(pos)
        if refreshNum > 0 then
            self._ui.btn_refresh:setVisible(true)
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
